$(function () {
  $.ajax({
    url: "http://chst.vip:1234/api/getbaicaijiatitle",
    data: {
      pageid: 1,
    },
    success: (res) => {
      let htm = "";
      $.each(res.result, (i, item) => {
        htm += `
            <li titleId='${item.titleId}'>${item.title}</li>
            `;
      });
      $("#selection ul").html(htm);
    },
  });
  //点击切换筛选
  $("#selection ul").on("click", "li", function () {
    filterRender($(this).attr("titleId"));
    $(this).css("color", "red").siblings().css("color", "black");
  });
  //商品列表
  filterRender(0);
  function filterRender(titleid) {
    $.ajax({
      url: "http://chst.vip:1234/api/getbaicaijiaproduct",
      data: {
        titleid,
      },
      success: (res) => {
        let htm = "";
        $.each(res.result, (i, item) => {
          htm += ` 
            <li>
            <a href="" class="clearfix im">
              <div class="img fl">
                ${item.productImg}
              </div>
              <div class="infoR">
                <div class="title">
                  <h4>
                    ${item.productName}
                  </h4>
                  <div class="price">
                    ${item.productPrice}
                    <del>1888.0</del>
                  </div>
                </div>
                <div class="down">
                  ${item.productCouponRemain}
                  <div class="click">
                    ${item.productCoupon}
                    ${item.productHref}
                  </div>
                </div>
              </div>
            </a>
            </li>`;
        });
        $("main ul").html(htm);

        res.result.forEach((item, i) => {
          let num = Math.ceil(Math.random() * 60);

          let t = setInterval(() => {
            $("div.bar")
              .eq(i)
              .css(
                "background",
                `linear-gradient(to right, red ${num}%,#ffb7b7 0%)`
              )
              .find("span")
              .text(`${num}%`)
              .end()
              .find("b")
              .text(`已领${num * 10}张`);
            if (num === 100) clearInterval(t);
            num++;
          }, 1000);
        });
      },
    });
  }

  //商品列表
  //   http://chst.vip:1234/api/getbaicaijiaproduct?titleid=0
});
